package cn.wolfcode.trip.app.web.controller;

import cn.wolfcode.trip.base.domain.Answer;
import cn.wolfcode.trip.base.query.QuestionnaireQueryObject;
import cn.wolfcode.trip.base.service.IAtWhoService;
import cn.wolfcode.trip.base.service.IQuestionnaireService;
import cn.wolfcode.trip.base.util.JSONResult;
import com.github.pagehelper.PageInfo;
import freemarker.template.Configuration;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对问卷调查的处理
 */
@Controller
@RequestMapping("/questionnaires")
public class QuestionnaireController {


    @Autowired
    private IQuestionnaireService questionnaireService;

    @Autowired
    private ServletContext ctx;

    /**
     * 查询调查的内容(问题和答案)
     * @param userId 用户id
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Object list(Long userId){
        return questionnaireService.selectQuestionnaire(userId);
    }

    @GetMapping("/getQuestionnaireId")
    @ResponseBody
    public List getQuestionnaireId(){
        return questionnaireService.getQuestionnaireId();
    }

    //导航页签数据的准备
    //由于接口的路径相同可以通过请求头进行接口访问的区分
    @GetMapping("/answer")
    public void getStrategiesByRegionIdHtml(HttpServletResponse resp)
            throws Exception {
        //问题
        List question = questionnaireService.selectProblems();
        //答案
        List<Answer> answer = questionnaireService.selectAnswer();


        //通过Freemarker配置对象进行模板数据的渲染
        Configuration cf = new Configuration(Configuration.VERSION_2_3_28);
        cf.setDirectoryForTemplateLoading(new File(ctx.getRealPath("/templates")));
        //获取模板文件
        Template template = cf.getTemplate("questionnaire.ftl","UTF-8");

        Map<String,Object> map = new HashMap<String, Object>();
        map.put("question",question );
        map.put("answer",answer );

        template.process(map, resp.getWriter());
    }

    /**
     * 用于保存和修改用户传回来的调查信息
     * @param userId 用户ID
     * @param value 题目和答案组成的字符串
     * @return
     */
    @PostMapping("/saveOrUpdate")
    @ResponseBody
    public void saveOrUpdate(Long userId,String value){
        questionnaireService.saveOrUpdate(userId,value);
    }

    /**
     * 用于保存和修改用户传回来的意见和建议
     * @param value 用户填写的意见信息
     * @return
     */
    @PostMapping("/saveEvaluates")
    @ResponseBody
    public Object saveEvaluates(Long userId,String value){
        JSONResult result = new JSONResult();
        if (value==null || value.length()==0){
            result.mark("请输入您的意见或建议!");
        }else {
            try {
                questionnaireService.saveEvaluates(userId, value);
            } catch (Exception e) {
                result.mark(e.getMessage());
            }
        }
        return result;
    }
}
